﻿#region
/*******************************************************************************
** File Name:		    ProductService.cs
** Creator:		        chao.jiang 
** EMail:               chao.jiang 
** Create date:	        
** Latest Modifier:
** Latest Modify date:
** Description:		    
** Modify History:
**
** Code Review:
**
** Version number:       1.0.0
*********************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using Phoenix.Service.Contracts;
using Phoenix.Service.Model;
using Phoenix.IBatisRepository;
using Phoenix.Respository;
using System.Data;
using Phoenix.Entity;

namespace Phoenix.Service
{
    public class ProductService : IProductService
    {
		 IProduct product = new ProductRepository();
		 
		 #region  Auto Generation
		 
        public void AddProduct(ProductModel model)
        {
			EProduct entity = ConvertModelToEntity(model);
			
            product.AddProduct(entity);
        }

		public void UpdateByProductID (ProductModel model)
		{
			EProduct entity = ConvertModelToEntity(model);

            product.UpdateByProductID(entity);

		}

		public void DeleteByProductID (System.Int32 productID)
		{
		
            product.DeleteByProductID(productID);

		}
		 
        public ProductModel GetByProductID (System.Int32 productID)
        {
        
			EProduct entity = product.GetByProductID(productID);
			
            ProductModel model = ConvertEntityToModel(entity);


            return model;
        }
        
	
		public IList<ProductModel> GetAll ()
		{
			IList<EProduct> entity = product.GetAll();
			
			if (entity == null)
            {
                return null;
            }
            
            IList<ProductModel> list = new List<ProductModel>();

            foreach (var item in entity)
            {
                ProductModel model = ConvertEntityToModel(item);
                list.Add(model);
            }
            return list;   
			
		}
		
		private ProductModel ConvertEntityToModel(EProduct entity)
		{
			if (entity == null)
            {
                return null;
            }
			ProductModel model = new ProductModel();
                        model.ProductID = entity.ProductID;
                        model.Title = entity.Title;
                        model.SEOKeywords = entity.SEOKeywords;
                        model.SEODesc = entity.SEODesc;
                        model.Summary = entity.Summary;
                        model.Description = entity.Description;
                        model.ImagePath = entity.ImagePath;
                        model.Visits = entity.Visits;
                        model.Likes = entity.Likes;
                        model.Comments = entity.Comments;
                        model.CategoryID = entity.CategoryID;
                        model.CategoryIDPath = entity.CategoryIDPath;
                        model.AreaID = entity.AreaID;
                        model.AreaIDPath = entity.AreaIDPath;
                        model.TaoKe = entity.TaoKe;
                        model.State = entity.State;
                        model.CreateTime = entity.CreateTime;
                        model.UpdateTime = entity.UpdateTime;
                        return model;
		}
		
		private EProduct ConvertModelToEntity(ProductModel model)
		{
			if (model == null)
            {
                return null;
            }
			EProduct entity = new EProduct();
                        entity.ProductID = model.ProductID;
                        entity.Title = model.Title;
                        entity.SEOKeywords = model.SEOKeywords;
                        entity.SEODesc = model.SEODesc;
                        entity.Summary = model.Summary;
                        entity.Description = model.Description;
                        entity.ImagePath = model.ImagePath;
                        entity.Visits = model.Visits;
                        entity.Likes = model.Likes;
                        entity.Comments = model.Comments;
                        entity.CategoryID = model.CategoryID;
                        entity.CategoryIDPath = model.CategoryIDPath;
                        entity.AreaID = model.AreaID;
                        entity.AreaIDPath = model.AreaIDPath;
                        entity.TaoKe = model.TaoKe;
                        entity.State = model.State;
                        entity.CreateTime = model.CreateTime;
                        entity.UpdateTime = model.UpdateTime;
                        return entity;
		}
		
		
		
		#endregion

        public void UpdateTaoKe(int productID, bool taoKe)
        {
            product.UpdateTaoKe(productID, taoKe);
        }
    }
}